class CMyHeap
{
public:
	CMyHeap(int a[])
	{}
	CMyHeap(int* a, int len)
	{
	this->len=len;
	this->data=new int[len];
	memcpy(this->data,a,len*4);
	this->heapLen=1;
	}
private:
	int len;
	int * data;
	int heapLen;
public:
	void Sort()
	{

		for(int i=1; i<this->len;i++)
		{
			
			this->AdjustHeapBottom(i);
			this->heapLen++;
		}
		for(int i=0; i<this->len;i++)
		{
			out(this->data[i]);
		}	
		for(int i=0; i < this->len-1; i++)
		{
			this->heapLen--;	
			swap(0, this->len-i-1);
			this->AdjustHeapBoot(0);
		
		}
	}
	int operator [](int i)
	{
		return this->data[i];
	}
	void (*out) (int);
private:
	void AdjustHeapBoot(int bootIndex)
	{
		if(bootIndex >= this->heapLen) return;
		int lchild=(bootIndex+1)*2-1;
		int child=lchild + 1;
		if(lchild >= this->heapLen) return;
		if(Compare(this->data[lchild],this->data[child]) || child >= this->heapLen)
		{
			child = lchild;
		}
		if(Compare(data[child] , data[bootIndex]))
		{
			swap(bootIndex, child);
			this->AdjustHeapBoot(child);
		}	
	}
	void AdjustHeapBottom(int index)
	{
		if(index<=0) return;
		int p=(index+1)/2;
		p--;
		if(Compare(data[index],data[p]))
		{
			swap(p,index);
			AdjustHeapBottom(p);
		}
	}
	void swap(int index1, int index2)
	{
		int temp=this->data[index1];
		this->data[index1] = this->data[index2];
	       this->data[index2]=temp;	
	}
protected:
	virtual bool Compare(int v1, int v2)
	{
		return v1 > v2;
	}

};

class MinHeap : public CMyHeap
{
public:
	MinHeap(int* a, int len):CMyHeap(a, len)
	{
	
	}
protected:
	bool Compare(int a, int b)
	{
		return a>b;
	}
};
